package com.yenlien.traceyun.plant.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.dao.ProcessDetailDao;
import com.yenlien.traceyun.business.dao.ProcessMaterialDao;
import com.yenlien.traceyun.business.dao.StorageManageDao;
import com.yenlien.traceyun.business.dao.StorageManageDetailedDao;
import com.yenlien.traceyun.business.entity.ProcessDetail;
import com.yenlien.traceyun.business.entity.ProcessMaterial;
import com.yenlien.traceyun.business.entity.StorageManage;
import com.yenlien.traceyun.business.entity.StorageManageDetailed;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import com.yenlien.traceyun.common.utils.Tools;
import com.yenlien.traceyun.plant.dao.ProcessDao;
import com.yenlien.traceyun.plant.dao.ProcessTechnologyDao;
import com.yenlien.traceyun.plant.dao.ReapDetailDao;
import com.yenlien.traceyun.plant.entity.Process;
import com.yenlien.traceyun.plant.entity.ProcessTechnology;
import com.yenlien.traceyun.plant.entity.ReapDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author master yi
 */
//@Service
public class ProcessService_bak {
    @Autowired
    private ProcessDao processDao;
    @Autowired
    private ReapDetailDao reapDetailDao;
    @Autowired
    private ProcessMaterialDao processMaterialDao;
    @Autowired
    private ProcessTechnologyDao processTechnologyDao;
    @Autowired
    private ProcessDetailDao processDetailDao;
    @Autowired
    private StorageManageDao storageManageDao;
    @Autowired
    private StorageManageDetailedDao storageManageDetailedDao;
    @Autowired
    private FileRestService fileService;


    @Transactional(rollbackFor = RuntimeException.class)
    public void save(List<MultipartFile> files, Process process, List<ProcessMaterial> materialList, List<ProcessTechnology> technologyList, List<ProcessDetail> detailList) throws Exception {
        processDao.save(process);
        List<ReapDetail> reapDetailList = new ArrayList<>();
        List<StorageManage> storageManageList = new ArrayList<>();
        List<StorageManageDetailed> storageManageDetailedList = new ArrayList<>();

        //使用原料
        for (ProcessMaterial material : materialList) {
            String type = material.getType();
            material.setProcessId(process.getId());
            if (("基地采收").equals(type)) {
                ReapDetail reapDetail = reapDetailDao.findById(material.getBreedId());
                reapDetail.setSurplusNumber(Tools.subtr(reapDetail.getSurplusNumber(), material.getNumber()));
                reapDetailList.add(reapDetail);
            } else if (("采购入库").equals(type)) {
                StorageManage storageManage = storageManageDao.findById(material.getBreedId());
//				storageManage.setSurplus(Tools.subtr(storageManage.getSurplus(), material.getNumber()));
//				storageManage.setSubtotal(Tools.subtr(storageManage.getSubtotal(), material.getNumber()));
                storageManageList.add(storageManage);

                StorageManageDetailed storageManageDetailed = new StorageManageDetailed();
                storageManageDetailed.setStorageManageId(storageManage.getId());
                storageManageDetailed.setType("加工");
                storageManageDetailed.setSpecs(storageManage.getSpecs());
                storageManageDetailed.setNumber(new BigDecimal(material.getNumber()));
                storageManageDetailedList.add(storageManageDetailed);
            }
        }
        processMaterialDao.saveAll(materialList);
        reapDetailDao.saveAll(reapDetailList);
        storageManageDao.saveAll(storageManageList);
        storageManageDetailedDao.saveAll(storageManageDetailedList);

        //===============工艺===============
        for (ProcessTechnology technology : technologyList) {
            fileService.upload(null, technology, files);
            technology.setProcessId(process.getId());
        }
        processTechnologyDao.saveAll(technologyList);

        //===============成品===============
        for (ProcessDetail detail : detailList) {
            detail.setProcessId(process.getId());
            detail.setSurplusNumber(detail.getTotalNumber());
        }
        processDetailDao.saveAll(detailList);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void save(Process process, List<MultipartFile> files) throws Exception {
        processDao.save(process);

        List<ProcessMaterial> materialList = new ArrayList<>();
        List<ProcessTechnology> technologyList = new ArrayList<>();
        List<ProcessDetail> detailList = new ArrayList<>();

        JSONArray pmList = JSONArray.parseArray(process.getPmList());
        JSONArray ptList = JSONArray.parseArray(process.getPtList());
        JSONArray pdList = JSONArray.parseArray(process.getPdList());

        if (pmList != null) {
            materialList = JSONArray.parseArray(pmList.toJSONString(), ProcessMaterial.class);
        }
        if (ptList != null) {
            technologyList = JSONArray.parseArray(ptList.toJSONString(), ProcessTechnology.class);
        }
        if (pdList != null) {
            detailList = JSONArray.parseArray(pdList.toJSONString(), ProcessDetail.class);
        }

        //===============原料===============
        List<ReapDetail> reapDetailList = new ArrayList<>();
        List<StorageManage> storageManageList = new ArrayList<>();
        List<StorageManageDetailed> storageManageDetailedList = new ArrayList<>();
        int i = 0;
        for (ProcessMaterial material : materialList) {
            String type = material.getType();
            ProcessDetail processDetail = detailList.get(i);
            material.setProcessId(process.getId());
            if (("基地采收").equals(type)) {
                ReapDetail reapDetail = reapDetailDao.findById(material.getBreedId());
                reapDetail.setSurplusNumber(Tools.subtr(reapDetail.getSurplusNumber(), material.getNumber()));
                reapDetailList.add(reapDetail);

                processDetail.setCrossPoint("trace");
                processDetail.setLastStep(reapDetail.getId());
            } else if (("采购入库").equals(type)) {
                StorageManage storageManage = storageManageDao.findById(material.getBreedId());
//				storageManage.setSurplus(Tools.subtr(storageManage.getSurplus(), material.getNumber()));
//				storageManage.setSubtotal(Tools.subtr(storageManage.getSubtotal(), material.getNumber()));
                storageManageList.add(storageManage);

                StorageManageDetailed storageManageDetailed = new StorageManageDetailed();
                storageManageDetailed.setStorageManageId(storageManage.getId());
                storageManageDetailed.setType("加工");
                storageManageDetailed.setSpecs(storageManage.getSpecs());
//				storageManageDetailed.setNumber(material.getNumber());
                storageManageDetailedList.add(storageManageDetailed);
                processDetail.setCrossPoint("purchase");
                processDetail.setLastStep(storageManage.getId());
            }
            i++;
        }
        reapDetailDao.saveAll(reapDetailList);
        storageManageDao.saveAll(storageManageList);
        storageManageDetailedDao.saveAll(storageManageDetailedList);

        //===============工艺===============
        for (ProcessTechnology technology : technologyList) {
            fileService.upload(null, technology, files);
            technology.setProcessId(process.getId());
        }

        //===============成品===============
        for (ProcessDetail detail : detailList) {
            detail.setProcessId(process.getId());
            detail.setSurplusNumber(detail.getTotalNumber());
        }
        processMaterialDao.saveAll(materialList);
        processTechnologyDao.saveAll(technologyList);
        processDetailDao.saveAll(detailList);
    }

    public JSONObject findById(String id) {
        JSONObject json = new JSONObject();

        List<ProcessMaterial> materialList = processMaterialDao.findByProcessId(id);
        json.put("materialList", materialList);

        List<ProcessTechnology> technologyList = processTechnologyDao.findByProcessId(id);
        json.put("technologyList", technologyList);

        List<ProcessDetail> detailList = processDetailDao.findByProcessId(id);
        json.put("detailList", detailList);

        return json;
    }
}
